package com.apobates.jforum2.troll.nest.service;

import com.apobates.jforum2.troll.nest.FrontFeignClientConfig;
import com.apobates.jforum2.troll.nest.service.PostsClient.PostsFallbackFactory;
import com.apobates.jforum2.troll.replies.stub.vo.ReplyMoodVo;
import com.apobates.jforum2.troll.replies.stub.vo.ReplyVo;
import com.apobates.jforum2.troll.utils.core.EntityPageResult;
import java.util.Collections;
import java.util.List;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 回复服务客户端
 * @author xiaofanku
 * @since 20210413
 */
@FeignClient(value="TROLL-GATEWAY",
            contextId="postsClient",
            fallbackFactory=PostsFallbackFactory.class,
            configuration = FrontFeignClientConfig.class
)
@RequestMapping(value="/posts")
public interface PostsClient {
    //4:查看指定回复
    @GetMapping("/{id}")
    ResponseEntity<ReplyVo> get(@PathVariable("id")long postsId);
    
    //5(移至Nest)
    
    //6:统计指定回复者在话题中的回复次数, 1楼不在统计之列.忽略状态值
    @GetMapping("/replies/{topic}/{member}")
    long countRepliesSize(@PathVariable("topic") long topicId, @PathVariable("member") long memberId);
    
    //7:查看指定话题最近的回复,忽略状态值;以entryDateTime倒序 服务于策略检查
    @GetMapping("/recent/{topic}")
    List<ReplyVo> getRecentForTopicIgnoreStatus(@PathVariable("topic") long topicId, @RequestParam("size") int minInterruptSize);
    
    //8:移除回复
    @PutMapping(value="/{id}/remove", consumes="application/x-www-form-urlencoded")
    boolean delReply(@PathVariable("id") long postsId,
              @RequestParam("member") long memberId,
              @RequestParam("nickname") String memberNickname,
              @RequestParam("token") String token,
              @RequestParam("ip") String ipAddr);
    
    //9:创建回复
    @PostMapping(value="/create", consumes="application/x-www-form-urlencoded")
    ResponseEntity<ReplyVo> createReply(
            @RequestParam("volumes") int volumesId,
            @RequestParam("board") long boardId,
            @RequestParam("threads") long topicId,
            @RequestParam("quote") long quotePostsId,
            @RequestParam("content") String content,
            @RequestParam("scale") String scale,
            @RequestParam("member") long memberId,
            @RequestParam("nickname") String memberNickname,
            @RequestParam("token") String token,
            @RequestParam("ip") String ipAddr);
    
    //10:查看指定的回复, 并对内容进行解码(编辑内容)
    @GetMapping("/{id}/content")
    ResponseEntity<ReplyVo> getPostsContentForEdit(@PathVariable("id") long postsId);
    
    //11:查看话题最近的回复,不包括楼主的回复,并对回复内容进行解码(RSS)
    @GetMapping("/rss/{topic}")
    List<ReplyVo> getRecentForRSS(@PathVariable("topic") long topicId, @RequestParam("size") int showSize);
    
    //12:查看话题中指定会员的所有回复,不包括楼主的话题内容并对回复内容进行图片解码
    @GetMapping("/threads/{topic}/{filterMember}/{scale}/{page}/{number}")
    EntityPageResult<ReplyVo> getRepliesOrFilterAuthor(
            @PathVariable("topic") long topicId,
            @PathVariable("filterMember") long filterMemberId,
            @PathVariable("scale") String imageScale,
            @PathVariable("page") int page,
            @PathVariable("number") int pageSize,
            @RequestParam(value="sort", required=false, defaultValue="0")int sortCategory);
    //13
    
    //14:编辑回复
    @PutMapping(value="/{id}/modify", consumes="application/x-www-form-urlencoded")
    ResponseEntity<ReplyVo> modifyReply(@PathVariable("id") long postsId,
                     @RequestParam("content") String content,
                     @RequestParam("member") long memberId,
                     @RequestParam("nickname") String memberNickname,
                     @RequestParam("token") String token,
                     @RequestParam("ip") String ipAddr);
    
    //15:创建喜好记录.若会员已对回复的喜好记录存在方法会删除并返回成功
    //-1表示删除喜好,大于0表示喜好增加成功,0喜好增加失败
    //200 表示操作成功(DEL表示删除,ADD表示增加), 417 表示操作失败
    @PostMapping(value="/{id}/mood", consumes="application/x-www-form-urlencoded")
    ResponseEntity<String> toggleMoodRecord(@PathVariable("id") long postsId,
                        @RequestParam("threads")long topicId,
                        @RequestParam("mood") int moodStatus,
                        @RequestParam("member") long memberId,
                        @RequestParam("nickname") String memberNickname,
                        @RequestParam("token") String token,
                        @RequestParam("ip") String ipAddr);
    
    //16:查看指定回复的喜好记录
    @GetMapping("/threads/{topic}/{id}/mood")
    ResponseEntity<ReplyMoodVo> getMoodRecordByPosts(@PathVariable("id") long postsId, @PathVariable("topic") long topicId);
    
    //17:查看指定的回复喜好汇总记录
    @PostMapping(value="/mood/query", consumes="application/x-www-form-urlencoded")
    List<ReplyMoodVo> getByPosts(@RequestParam("threads")long topicId, @RequestParam("records") String postsIdString);
    
    //18:查看指定话题的内容
    @GetMapping("/threads/{topic}/content")
    ResponseEntity<ReplyVo> getOneFloorContent(@PathVariable("topic") long topicId);
    
    //19:查看指定话题的内容并解码内容中的图片
    @GetMapping("/threads/{topic}/content/decode")
    ResponseEntity<ReplyVo> getOneFloorDecodeContent(@PathVariable("topic") long topicId, @RequestParam(value="scale", required=false, defaultValue="auto") String imageScale);
    
    @Component
    static class PostsFallbackFactory implements FallbackFactory<PostsClientFallback> {
        @Override
        public PostsClientFallback create(Throwable cause) {
            return new PostsClientFallback();
        }
    }
    
    static class PostsClientFallback implements PostsClient{

        @Override
        public ResponseEntity<ReplyVo> get(long postsId) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public long countRepliesSize(long topicId, long memberId) {
            return -1L;
        }

        @Override
        public List<ReplyVo> getRecentForTopicIgnoreStatus(long topicId, int minInterruptSize) {
            return Collections.emptyList();
        }

        @Override
        public boolean delReply(long postsId, long memberId, String memberNickname, String token, String ipAddr) {
            return false;
        }

        @Override
        public ResponseEntity<ReplyVo> createReply(int volumesId, long boardId, long topicId, long quotePostsId, String content, String scale, long memberId, String memberNickname, String token, String ipAddr) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<ReplyVo> getPostsContentForEdit(long postsId) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public List<ReplyVo> getRecentForRSS(long topicId, int showSize) {
            return Collections.emptyList();
        }

        @Override
        public ResponseEntity<ReplyVo> modifyReply(long postsId, String content, long memberId, String memberNickname, String token, String ipAddr) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<String> toggleMoodRecord(long postsId, long topicId, int moodStatus, long memberId, String memberNickname, String token, String ipAddr) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<ReplyMoodVo> getMoodRecordByPosts(long postsId, long topicId) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public List<ReplyMoodVo> getByPosts(long topicId, String postsIdString) {
            return Collections.emptyList();
        }

        @Override
        public EntityPageResult<ReplyVo> getRepliesOrFilterAuthor(long topicId, long filterMemberId, String imageScale, int page, int pageSize, int sortCategory) {
            return EntityPageResult.empty(1);
        }

        @Override
        public ResponseEntity<ReplyVo> getOneFloorContent(long topicId) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<ReplyVo> getOneFloorDecodeContent(long topicId, String imageScale) {
            return ResponseEntity.notFound().build();
        }
        
    }
}